<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getOrderDetailAPI } from '@/apis/order'

const route = useRoute()
const router = useRouter()
const orderId = route.params.id

// 订单状态翻译
const orderStateMap = {
  1: '待付款',
  2: '待发货',
  3: '待收货',
  4: '待评价',
  5: '已完成',
  6: '已取消'
}

// 订单详情数据
const orderDetail = ref({})
const loading = ref(true)

// 获取订单详情
const getOrderDetail = async () => {
  loading.value = true
  try {
    const res = await getOrderDetailAPI(orderId)
    if (res.data && res.data.code === 200 && res.data.result) {
      orderDetail.value = res.data.result
    } else {
      ElMessage.error('获取订单详情失败')
    }
  } catch (error) {
    console.error('获取订单详情出错:', error)
    ElMessage.error('获取订单详情出错')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  if (orderId) {
    getOrderDetail()
  } else {
    ElMessage.error('订单ID无效')
    router.push('/member/order')
  }
})

// 返回订单列表
const goBack = () => {
  router.push('/member/order')
}
</script>

<template>
  <div class="order-detail-page">
    <div class="container">
      <div class="detail-container">
        <div class="header">
          <el-page-header title="返回" @back="goBack">
            <template #content>
              <span class="title">订单详情</span>
            </template>
          </el-page-header>
        </div>
        
        <div v-if="loading" class="loading">
          <el-skeleton style="width: 100%" :rows="10" animated />
        </div>
        
        <div v-else>
          <!-- 订单状态 -->
          <div class="order-status">
            <div class="status-icon">
              <span class="iconfont" :class="{'icon-queren2': orderDetail.state !== 6, 'icon-shanchu': orderDetail.state === 6}"></span>
            </div>
            <div class="status-text">
              <h3>{{ orderStateMap[orderDetail.state] }}</h3>
              <p v-if="orderDetail.state === 1">
                请在{{ orderDetail.countdown || 0 }}秒内支付，超时订单将自动取消
              </p>
              <p v-else-if="orderDetail.state === 2">商家正在备货中，请耐心等待</p>
              <p v-else-if="orderDetail.state === 3">商品已发货，请注意查收</p>
              <p v-else-if="orderDetail.state === 4">已收货，请尽快评价</p>
              <p v-else-if="orderDetail.state === 5">订单已完成</p>
              <p v-else-if="orderDetail.state === 6">订单已取消</p>
            </div>
          </div>
          
          <!-- 收货信息 -->
          <div class="detail-section">
            <h3 class="section-title">收货信息</h3>
            <div class="section-content">
              <p><span>收 货 人：</span>{{ orderDetail.receiver }}</p>
              <p><span>联系方式：</span>{{ orderDetail.contact }}</p>
              <p><span>收货地址：</span>{{ orderDetail.address }}</p>
            </div>
          </div>
          
          <!-- 订单信息 -->
          <div class="detail-section">
            <h3 class="section-title">订单信息</h3>
            <div class="section-content">
              <p><span>订单编号：</span>{{ orderDetail.id }}</p>
              <p><span>下单时间：</span>{{ orderDetail.createTime }}</p>
              <p><span>支付方式：</span>在线支付</p>
            </div>
          </div>
          
          <!-- 商品信息 -->
          <div class="detail-section">
            <h3 class="section-title">商品信息</h3>
            <el-table :data="orderDetail.orderItems || []" style="width: 100%">
              <el-table-column label="商品信息">
                <template #default="{ row }">
                  <div class="goods-info">
                    <img :src="row.image" alt="商品图片">
                    <div>
                      <p>{{ row.name }}</p>
                      <p class="attrs">{{ row.attrsText }}</p>
                    </div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="price" label="单价" width="120">
                <template #default="{ row }">¥{{ row.realPay / row.quantity }}</template>
              </el-table-column>
              <el-table-column prop="quantity" label="数量" width="120" />
              <el-table-column prop="realPay" label="小计" width="120">
                <template #default="{ row }">¥{{ row.realPay }}</template>
              </el-table-column>
            </el-table>
          </div>
          
          <!-- 订单金额 -->
          <div class="detail-section price-section">
            <div class="price-box">
              <dl>
                <dt>商品件数：</dt>
                <dd>{{ orderDetail.totalNum || 0 }}件</dd>
              </dl>
              <dl>
                <dt>商品总价：</dt>
                <dd>¥{{ (orderDetail.payMoney - orderDetail.postFee) || 0 }}</dd>
              </dl>
              <dl>
                <dt>运费：</dt>
                <dd>¥{{ orderDetail.postFee || 0 }}</dd>
              </dl>
              <dl class="total">
                <dt>应付总额：</dt>
                <dd>¥{{ orderDetail.payMoney || 0 }}</dd>
              </dl>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.order-detail-page {
  padding: 20px 0;
  background-color: #f5f5f5;
  
  .container {
    width: 1240px;
    margin: 0 auto;
  }
  
  .detail-container {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    
    .header {
      margin-bottom: 20px;
      
      .title {
        font-size: 16px;
        font-weight: bold;
      }
    }
    
    .loading {
      padding: 20px 0;
    }
    
    .order-status {
      display: flex;
      padding: 30px 0;
      background-color: #f7f7f7;
      margin-bottom: 20px;
      
      .status-icon {
        width: 80px;
        text-align: center;
        
        .iconfont {
          font-size: 40px;
          color: #27BA9B;
          
          &.icon-shanchu {
            color: #999;
          }
        }
      }
      
      .status-text {
        flex: 1;
        
        h3 {
          font-size: 18px;
          margin-bottom: 10px;
        }
        
        p {
          color: #999;
        }
      }
    }
    
    .detail-section {
      margin-bottom: 20px;
      
      .section-title {
        font-size: 16px;
        font-weight: normal;
        padding: 10px 0;
        border-bottom: 1px solid #f5f5f5;
      }
      
      .section-content {
        padding: 15px 0;
        
        p {
          line-height: 30px;
          
          span {
            margin-right: 20px;
            color: #999;
          }
        }
      }
      
      &.price-section {
        border-top: 1px solid #f5f5f5;
        padding-top: 20px;
      }
      
      .price-box {
        width: 400px;
        margin-left: auto;
        
        dl {
          display: flex;
          line-height: 30px;
          
          dt {
            width: 100px;
            color: #999;
          }
          
          dd {
            flex: 1;
            text-align: right;
          }
          
          &.total {
            border-top: 1px solid #f5f5f5;
            padding-top: 10px;
            margin-top: 10px;
            
            dt,dd {
              font-size: 18px;
              color: #FF6600;
            }
          }
        }
      }
    }
    
    .goods-info {
      display: flex;
      
      img {
        width: 70px;
        height: 70px;
        object-fit: cover;
        margin-right: 10px;
      }
      
      .attrs {
        color: #999;
        font-size: 12px;
        margin-top: 5px;
      }
    }
  }
}
</style> 